package IniTester;

import inireader.*;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
 * @author Vladimír Matěna
 */
public class Example2
{
	public static void main(String[] args) throws SAXException, IOException,
			ParserConfigurationException
	{
		/**
		 * Example2
		 * 
		 * This is example of advanced configurator usage.
		 * In this example config validated by schema is read from file
		 * modified and then stored to file again. Config validation will be
		 * done in strict mode when all elements have types specified by schema.
		 */

		/**
		 * In the beginning we need to create configuration from schema file
		 * and read it's content from file. Strict validating is set as we want
		 * config file match exactly our schema.
		 */
		Configurator config = new Configurator("Example2.xml", Validity.Strict);
		config.loadINI("Example2.in.ini");

		/**
		 * At this point schema is loaded and we can read data from it.
		 * This code writes some values to standard output.
		 */
		String Option1 = config.getSection("Section1")
				.getProperty("Option1")
				.getElementOnIndex(0).toEnum();
		System.out.println(String.format("Option1:%s", Option1));

		String Text2 = config.getSection("Section1")
				.getProperty("Text2")
				.getElementOnIndex(0).toString();
		System.out.println(String.format("Text2:%s", Text2));

		int Hex = config.getSection("Numbers")
				.getProperty("integer_hex")
				.getElementOnIndex(1).toInt();
		System.out.println(String.format("Integer in hex:%x", Hex));

		/**
		 * Even nonmandatory section not included in file can be read.
		 * It will return default value stored in schema.
		 */
		String Optional = config.getSection("NonMandatory")
				.getProperty("Optional")
				.getElementOnIndex(0).toString();
		System.out.println(String.format("Optional:%s", Optional));

		/**
		 * This shows how to change values. We will change Option1 to value2
		 * and Optional to Hello.
		 */
		config.getSection("Section1")
				.getProperty("Option1")
				.getElementOnIndex(0)
				.setUnnormalizedValue("value2");

		config.getSection("NonMandatory")
				.getProperty("Optional")
				.getElementOnIndex(0)
				.setUnnormalizedValue("Hello");

		/**
		 * Now we can save changed configuration back to file.
		 */
		config.saveINI("Example2.out.ini");
	}
}
